package com.wyj.interceptor;

import com.wyj.pojo.User;
import com.wyj.service.imp.DataServiceImp;
import com.wyj.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 王永杰
 * @Description 当用户访问其他资源的的时候  其的ip 就会被统计下来  统计结果为今天谁访问了网站
 *                同时也会将用户的userId进行统计  这样的话 就将我们的活跃的用户进行了统计
 * @since 2022/8/10
 */
@Component
public class DateInterceptor implements HandlerInterceptor {

    @Autowired
    DataServiceImp dataServiceImp;

    @Autowired
    HostHolder hostHolder;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String ip = request.getRemoteHost();
        dataServiceImp.recordUV(ip);

        User user = hostHolder.getUser();
        if (user != null) {
            dataServiceImp.recordDAU(user.getId());
        }

        return  true;
    }
}
